#ifndef MY_GPRS_H
#define MY_GPRS_H

#include <Arduino.h>

#include <avr/pgmspace.h> //PARA LA CLASE HELPER

typedef void (*command_callback)(void);

const char SMS_COMMAND_TOKEN[] =          "ELSIS";
const char SMS_COMMAND_PASSWORD[] =       "CLAVE";
const char SMS_COMMAND_ADD_NUMBER[] =     "NUEVO";
const char SMS_COMMAND_DELETE_NUMBER[] =  "BORRAR";
const char SMS_COMMAND_QUERY_STATUS[] =   "CONSULTA";

const int MAX_PHONE_NUMBERS = 5;

//EJEMPLOS DE COMANDOS VÁLIDOS
//    ELSIS PWD ADD 600123456
//    ELSIS PWD DELETE 600123456
//    ELSIS PWD QUERY 600123456



//NOTA: Esta clase sólo puede usarse con arduino Mega, porque usa el Serial1 (hardware)

class Gprs
{
public:

  //Encendido - apagado etc
  Gprs(uint8_t switchOnPin, uint8_t statusLedPin);
  boolean turnOn();
  void turnOff();
  void setSmsTxtMode();

  //Mensajes
  String readSmsAtIndex(int index);

  boolean sendSms(const char* phoneNumber, String msg);
  boolean sendSms(const char* phoneNumber, const char* msgTxt);

  boolean deleteSmsAtIndex(int index);
  //Llamadas
  void makeCall(const char* phoneNumber);
  //Comandos
  String checkReceivedCommand(int index);
  
  int getSignalStrength();


private:
  uint8_t m_switchOnPin;
  uint8_t m_statusLedPin;

  void ledBlink(int on, int off);

  void readAtCommandReply(char* reply, int replyLength);
  void readAtCommandReply(String command, char* reply, int replyLength);

};


class Helper
{
  // this function will return the number of bytes currently free in RAM
  // written by David A. Mellis
  // based on code by Rob Faludi http://www.faludi.com
public: 
  static int availableMemory()
  {
    int size = 8192; // Use 1024 with Arduino UNO; 2048 with ATmega328; 8192 with Mega 2560
    byte *buf;

    while ((buf = (byte *) malloc(--size)) == NULL);
    free(buf);
    return size;
  }

};


#endif

